﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ElevatorLib
{
    /// <summary>
    /// 基于OD矩阵的客流生成的配置参数
    /// </summary>
    public class ODConfig:XMLBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="url">配置文件路径</param>
        public ODConfig(string url)
            : base(url)
        {
 
        }

        /// <summary>
        /// 更新楼层数配置
        /// </summary>
        /// <param name="num">楼层数</param>
        /// <returns></returns>
        public bool UpdateFloorNum(int num)
        {
            return Update("/Root/Param/N", "", num.ToString());
        }

        /// <summary>
        /// 读取楼层数配置
        /// </summary>
        /// <returns>楼层数</returns>
        public int ReadFloorNum()
        {
            return Convert.ToInt32(Read("/Root/Param/N", ""));
        }

        /// <summary>
        /// 更新Lambda配置
        /// </summary>
        /// <param name="l">Lambda</param>
        /// <returns></returns>
        public bool UpdateLambda(int l)
        {
            return Update("/Root/Param/Lambda", "", l.ToString());
        }

        /// <summary>
        /// 读取Lambda配置
        /// </summary>
        /// <returns>Lambda</returns>
        public int ReadLambda()
        {
            return Convert.ToInt32(Read("/Root/Param/Lambda", ""));
        }

        /// <summary>
        /// 更新各楼层客流分布
        /// </summary>
        /// <param name="pop">客流量</param>
        /// <returns></returns>
        public bool UpdatePOP(int[] pop)
        {
            int num = ReadFloorNum();
            if (pop.Length != num)
            {
                return false;
            }
            string str = pop[0].ToString();
            for (int i = 1; i < pop.Length; i++)
            {
                str += "-" + pop[i];
            }
            return Update("/Root/Param/POP", "", str);
        }

        /// <summary>
        /// 读取各楼层客流分布
        /// </summary>
        /// <returns>客流量</returns>
        public int[] ReadPOP()
        {
            string str = Read("/Root/Param/POP", "");
            string[] strs = str.Split('-');
            int[] pop = new int[strs.Length];
            for (int i = 0; i < strs.Length; i++)
            {
                try
                {
                    pop[i] = Convert.ToInt32(strs[i]);
                }
                catch
                {
                    return null;
                }
            }
            return pop;
        }

        /// <summary>
        /// 更新交通流配置
        /// </summary>
        /// <param name="x">上行交通流</param>
        /// <param name="y">下行交通流</param>
        /// <param name="z">层间交通流</param>
        /// <returns></returns>
        public bool UpdateXYZ(double x,double y,double z)
        {
            if (1 != (x + y + z))
            {
                return false;
            }
            return Update("/Root/Param/X", "", x.ToString()) && Update("/Root/Param/Y", "", y.ToString()) && Update("/Root/Param/Z", "", z.ToString());
        }

        /// <summary>
        /// 读取交通流配置
        /// </summary>
        /// <param name="x">上行交通流</param>
        /// <param name="y">下行交通流</param>
        /// <param name="z">层间交通流</param>
        public void ReadXYZ(out double x,out double y,out double z)
        {
            x = Convert.ToDouble(Read("/Root/Param/X", ""));
            y = Convert.ToDouble(Read("/Root/Param/Y", ""));
            z = Convert.ToDouble(Read("/Root/Param/Z", ""));
        }
        
        /// <summary>
        /// 更新生成客流数量配置
        /// </summary>
        /// <param name="num">数量</param>
        /// <returns></returns>
        public bool UpdateNum(int num)
        {
            return Update("/Root/Num", "", num.ToString());
        }

        /// <summary>
        /// 读取生成客流数量配置
        /// </summary>
        /// <returns>数量</returns>
        public int ReadNum()
        {
            return Convert.ToInt32(Read("/Root/Num", ""));
        }
    }
}
